package org.example.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data;
import org.example.pojo.DApplyApartment;
import org.example.pojo.DOperationlog;
import org.example.pojo.select.DOperationLogSelect;
import org.example.service.DOperationlogService;
import org.example.mapper.DOperationlogMapper;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* @author chenhongkai
* @description 针对表【d_operationLog(操作日志表)】的数据库操作Service实现
* @createDate 2024-06-19 16:05:39
*/
@Service
public class DOperationlogServiceImpl extends ServiceImpl<DOperationlogMapper, DOperationlog>
    implements DOperationlogService{

    @Autowired
    private DOperationlogMapper dOperationlogMapper;

    @Override
    public Result<Map> SelectAllOperationLog(DOperationLogSelect dOperationLogSelect) {

        if(!Objects.equals(dOperationLogSelect.getOperationDate(),"") && !Objects.equals(dOperationLogSelect.getOperationDate(),null)){
            //字符串转时区
            Instant instant = Instant.parse(dOperationLogSelect.getOperationDate());
            //转换为特定时区的时间
            ZoneId zoneId = ZoneId.of("Asia/Shanghai");
            //特定时区的时间
            ZonedDateTime zdt = instant.atZone(zoneId);

            CustomDateTime customDateTime = new CustomDateTime(zdt);

            dOperationLogSelect.setCustomDateTime(customDateTime);
        }

        Page<DApplyApartment> page = new Page<>(dOperationLogSelect.getCurrentPage(),dOperationLogSelect.getPageSize());
        IPage<Map> mapIPage = dOperationlogMapper.SelectAllApplyApartment(page,dOperationLogSelect);

        Map dOperationlogMap = new HashMap<>();
        dOperationlogMap.put("pageSize",mapIPage.getSize());
        dOperationlogMap.put("pageTotal",mapIPage.getTotal());
        dOperationlogMap.put("logData",mapIPage.getRecords());

        return Result.ok(dOperationlogMap);
    }

    @Override
    public Result<Map> AddLog(DOperationlog dOperationlog) {
        int row = dOperationlogMapper.insert(dOperationlog);
        if(row == 0) return Result.error("添加日志失败");
        return Result.ok(null);
    }

    @Override
    public Result<Map> DeleteLog(DOperationlog dOperationlog) {
        int row = dOperationlogMapper.deleteById(dOperationlog);
        if(row == 0) return Result.error("删除日志失败");
        return Result.ok(null);
    }

    @Data
    public class CustomDateTime {
        private int year;
        private int month;
        private int day;
        private int hour;
        private int minute;
        private int second;
        public CustomDateTime(ZonedDateTime zdt) {
            this.year = zdt.getYear();
            this.month = zdt.getMonthValue(); // 注意：月份是从1开始的
            this.day = zdt.getDayOfMonth();
            this.hour = zdt.getHour();
            this.minute = zdt.getMinute();
            this.second = zdt.getSecond();
        }
    }

}




